<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$srnoSelected = 0;
	$customerArr = array();
	$supplierArr = array();
	$subCatArr   = array();
	$partyId     = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
	$supplierId  = isset($_POST['supplierId']) ? $_POST['supplierId'] : 0;
	$address     = isset($_POST['address']) ? nl2br(addslashes($_POST['address'])) : "";
	$srNo        = isset($_POST['srNo']) ? $_POST['srNo'] : 0;
	$complaintDate = "";
	$targetDate    = "";
	//insert for r and r :Start
	if(isset($_POST['submitBtn']) || isset($_POST['printBtn']))
	{
		$curDate = new DateTime();
    $curDate->setTimezone($cfgTimeZone);
    $timeset = $curDate->format('H:i:s');
		//$complaintDate = $delivaryDate = $_POST['complaintYear']."-".$_POST['complaintMonth']."-".$_POST['complaintDay']." ".$timeset;
		$complainDate   = date("Y-m-d")." ".$timeset;
		$targetDate     = $_POST['targetYear']."-".$_POST['targetMonth']."-".$_POST['targetDay']." ".$timeset;
		$insertInMaster = "INSERT INTO randrmaster(partyId,userId,srNo,complaintDate,targetDate,supplierId,address)
		                   VALUES (".$partyId.",".$_SESSION['s_activeUserId'.$cfgPack].",".$srNo.",'".$complainDate."','".$targetDate."',".$supplierId.",'".$address."')";
    $insertInMasterRes = mysql_query($insertInMaster);
    $lastInsertId = mysql_insert_id();		 
    if(!$insertInMasterRes)                               
    {
    	echo "Insert Query Error".mysql_error();
    }
    else
    {
    	foreach($_POST['subCategoryId'] as $key => $value)
    	{
    		$itemId        = isset($_POST['itemId'][$key]) ? $_POST['itemId'][$key] : 0;
    		$quantity      = isset($_POST['quantity'][$key]) ? $_POST['quantity'][$key] : "";
    		$complaintDesc = isset($_POST['complaintDesc'][$key]) ? $_POST['complaintDesc'][$key] : "";
        $insertInDetail = "INSERT INTO randrdetail(rAndRMasterId,subCategoryId,itemId,quantity,complaintDescription)
                                VALUES (".$lastInsertId.",".$value.",".$itemId.",".$quantity.",'".nl2br(addslashes($complaintDesc))."')";
        $insertInDetailRes = mysql_query($insertInDetail);                         
      }
    }
    
    if(isset($_POST['printBtn']))
    {
    	header("Location:changeStatusRnR.php?rAndRMasterId=".$lastInsertId."&print=1");
    }
    else
    {
    	header("Location:pendingRandRList.php");
    }
	}
	//insert for r and r :End
  //select Srno :Start
  $srnoQuery = "SELECT MAX(srNo) AS maxSrNO
                  FROM randrmaster
                 WHERE 1 = 1 ";
  $srnoQueryResult = mysql_query($srnoQuery);
  if($srnoRow = mysql_fetch_array($srnoQueryResult))
  {
    $srnoSelected   = $srnoRow['maxSrNO'] + 1;
  }
  //select Srno :End
  //select Customer :Start
  $selectCustomer = "SELECT partyId,companyName
                       FROM party
                   ORDER BY companyName";
  $selectCustomerRes = mysql_query($selectCustomer);
  $c = 0;
  while($customerRows = mysql_fetch_array($selectCustomerRes))
  {
  	$customerArr['partyId'][$c]     = $customerRows['partyId'];
  	$customerArr['companyName'][$c] = substr_replace($customerRows['companyName'],'...',40);
  	$c++;
  }                      
  //select Customer :End
  //select supplier :Start
  $selectSupplier = "SELECT supplierId,companyName
                       FROM supplier
                   ORDER BY companyName";
  $selectSupplierRes = mysql_query($selectSupplier);  
  $s = 0;
  while($supplierRows = mysql_fetch_array($selectSupplierRes))
  {
  	$supplierArr['supplierId'][$s]  = $supplierRows['supplierId'];
  	$supplierArr['companyName'][$s] = substr_replace($supplierRows['companyName'],'...',40);
  	$s++;
  }                 
  //select supplier :End
  //select subcategory :Start
  $selectSubCategory = "SELECT subCategoryId,subCategoryName
                          FROM subcategory
                      ORDER BY subCategoryName";
  $selectSubCategoryRes = mysql_query($selectSubCategory);
  $sc = 0;                      
  while($subCatRows = mysql_fetch_array($selectSubCategoryRes))
  {
  	$subCatArr['subCategoryId'][$sc]   = $subCatRows['subCategoryId'];
  	$subCatArr['subCategoryName'][$sc] = $subCatRows['subCategoryName'];
  	$sc++;
  }
  //select subcategory :End
  include("./bottom.php");
  $smarty->assign("srnoSelected",$srnoSelected);
  $smarty->assign("customerArr",$customerArr);
  $smarty->assign("supplierArr",$supplierArr);
  $smarty->assign("subCatArr",$subCatArr);
  $smarty->display("rAndR.tpl");
}
?>